home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Internet Info 1994 March
/
Internet Info CD-ROM (Walnut Creek) (March 1994).iso
/
networking
/
ip
/
ka9q
/
src890906.arc
/
TRACE.C
< prev
next >
Wrap
C/C++ Source or Header
|
1989-09-07
|
3KB
|
158 lines
#include <stdio.h>
#include <ctype.h>
#include "global.h"
#include "mbuf.h"
#include "iface.h"
#include "trace.h"
static void ascii_dump __ARGS((struct mbuf **bpp));
static void ctohex __ARGS((char *buf,int16 c));
static void fmtline __ARGS((int16 addr,char *buf,int16 len));
static void hex_dump __ARGS((struct mbuf **bpp));
/* Redefined here so that programs calling dump in the library won't pull
* in the rest of the package
*/
static char nospace[] = "No space!!\n";
void
dump(iface,direction,type,bp)
register struct iface *iface;
int direction;
unsigned type;
struct mbuf *bp;
{
struct mbuf *tbp;
void (*func) __ARGS((struct mbuf **,int));
int16 size;
if(iface == NULL || (iface->trace & direction) == 0)
return; /* Nothing to trace */
switch(direction){
case IF_TRACE_IN:
if((iface->trace & IF_TRACE_NOBC)
&& (Tracef[type].addrtest != NULLFP)
&& (*Tracef[type].addrtest)(iface,bp) == 0)
return; /* broadcasts are suppressed */
printf("%s recv:\n",iface->name);
break;
case IF_TRACE_OUT:
printf("%s sent:\n",iface->name);
break;
}
if(bp == NULLBUF || (size = len_mbuf(bp)) == 0){
printf("empty packet!!\n");
return;
}
if(type < NLTYPE)
func = Tracef[type].tracef;
else
func = NULLVFP;
dup_p(&tbp,bp,0,size);
if(tbp == NULLBUF){
printf(nospace);
return;
}
if(func != NULLVFP)
(*func)(&tbp,1);
if(iface->trace & IF_TRACE_ASCII){
/* Dump only data portion of packet in ascii */
ascii_dump(&tbp);
} else if(iface->trace & IF_TRACE_HEX){
/* Dump entire packet in hex/ascii */
free_p(tbp);
dup_p(&tbp,bp,0,len_mbuf(bp));
if(tbp != NULLBUF)
hex_dump(&tbp);
else
printf(nospace);
}
free_p(tbp);
}
/* Dump an mbuf in hex */
static void
hex_dump(bpp)
register struct mbuf **bpp;
{
int16 n;
int16 address;
char buf[16];
if(bpp == NULLBUFP || *bpp == NULLBUF)
return;
address = 0;
while((n = pullup(bpp,buf,sizeof(buf))) != 0){
fmtline(address,buf,n);
address += n;
}
}
/* Dump an mbuf in ascii */
static void
ascii_dump(bpp)
register struct mbuf **bpp;
{
char c;
register int16 tot;
if(bpp == NULLBUFP || *bpp == NULLBUF)
return;
tot = 0;
while(pullup(bpp,&c,1) == 1){
if((tot % 64) == 0)
printf("%04x ",tot);
putchar(isprint(c) ? c : '.');
if((++tot % 64) == 0)
printf("\n");
}
if((tot % 64) != 0)
printf("\n");
}
/* Print a buffer up to 16 bytes long in formatted hex with ascii
* translation, e.g.,
* 0000: 30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f 0123456789:;<=>?
*/
static void
fmtline(addr,buf,len)
int16 addr;
char *buf;
int16 len;
{
char line[80];
register char *aptr,*cptr;
register char c;
memset(line,' ',sizeof(line));
ctohex(line,hibyte(addr));
ctohex(line+2,lobyte(addr));
aptr = &line[6];
cptr = &line[55];
while(len-- != 0){
c = *buf++;
ctohex(aptr,(int16)uchar(c));
aptr += 3;
c &= 0x7f;
*cptr++ = isprint(c) ? c : '.';
}
*cptr++ = '\r';
*cptr++ = '\n';
fwrite(line,1,(unsigned)(cptr-line),stdout);
}
/* Convert byte to two ascii-hex characters */
static
void
ctohex(buf,c)
register char *buf;
register int16 c;
{
static char hex[] = "0123456789abcdef";
*buf++ = hex[hinibble(c)];
*buf = hex[lonibble(c)];
}